package com.sang.controller;

import com.sang.mapper.UserMapper;
import com.sang.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/queryUserList")
    public List<User> queryUserList(){
        List<User> userList = userMapper.queryUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        return userList;
    }

    //根据id查询一个用户
    @GetMapping("/queryUser/{id}")
    public User queryUserById(@PathVariable("id")int id) {
        User user = userMapper.queryUserById(1);
        return user;
    }

    //添加一个用户
    @RequestMapping("/addUser")
    public String addUser() {
        userMapper.addUser(new User(5,"阿毛","123456"));
        return "ok";
    }

    //更新一个用户
    @RequestMapping("/updateUser")
    public String updateUser() {
        userMapper.updateUser(new User(5,"阿毛2","123456"));
        return "ok";
    }

    //删除一个用户
    @RequestMapping("/deleteUser")
    public String deleteUser() {
        userMapper.deleteUser(5);
        return "ok";
    }

}